import {
	seacrhWarehousing,
	addAssetHandle,
	updateAssetHandle,
	getAssetHandle,
	checkDisposalOrderNo,
	checkDisposalOrderNoByUpdate
} from "@/views/wy-asset-management/handle/api/handle.js";
import assetDictionary from "@/views/wy-asset-management/mixin/assetDictionary";
import { mapGetters } from "vuex";
import { getDict } from "~/utils";

export default {
	mixins: [assetDictionary],
	data() {
		/**
		 * async / await 同步
		 * 校验处置单号的唯一性
		 */
		let validatorDisposalOrderNo = async (rule, value, callback) => {
			if (value == "") {
				callback(new Error("请输入处置单号"));
			}
			if (this.$route.query.assetHandleId == null) {
				//新增验证
				await checkDisposalOrderNo(value).then(respData => {
					let result = respData.data.data;
					if (result == false) {
						callback(new Error("处置单号重复"));
					}
				});
			} else {
				//修改验证
				await checkDisposalOrderNoByUpdate(
					value,
					this.$route.query.assetHandleId
				).then(respData => {
					let result = respData.data.data;
					if (result == false) {
						callback(new Error("处置单号重复"));
					}
				});
			}
		};

		return {
			activeName: "处置审核信息",
			cardNum: "不动产证编号",
			loading: true,
			//上传文件相关
			busId: this.$route.query.assetFixedWarehousingId,
			modelType: "asset-warehousing",
			url: "/admin/sys-file/upLodeFileEdit",
			fileList: [],
			//处置记录附件
			changeBusId: "",
			changeFileList: [],
			// 是否是政府项目
			government: 0,

			//弹出框 初始时隐藏
			dialogVisible: false,

			//入库信息
			formObj: {
				//基本信息
				assetFixedWarehousingId: this.$route.query.assetFixedWarehousingId,
				warehousingAttachment: this.$route.query.warehousingAttachment,
				roomId: this.$route.query.roomId,
				assetName: "",
				assetClassification: 0,
				useStatus: 0,
				acquisitionMethod: 0,
				acquisitionDate: "",
				investmentAcquisitionDate: "",
				valueType: 0,
				registrationPrice: 0,
				registrationFee: 0,
				taxation: 0,
				value: 0,
				number: "",
				measurementUnit: 0,
				landArea: 0,
				propertyRightForm: 0,
				ownershipCertificateNo: "",
				issueData: "",
				ownershipPeriod: "",
				ownershipCertificate: 0,
				location: "",
				ownershipNature: 0,
				selfUseArea: 0,
				selfUseValue: 0,
				buildingStructure: 0,
				voucherNo: "",
				voucherDateStart: "",
				voucherDateEnd: ""
			},
			//处置信息
			formObj2: {
				assetHandleId: this.$route.query.assetHandleId,
				assetFixedWarehousingId: this.$route.query.assetFixedWarehousingId,
				voucherNo: "",
				approveContent: "",
				voucherDateStart: "",
				voucherDateEnd: "",
				disposalOrderNo: "",
				disposalType: null,
				residualIncome: null,
				disposalReason: "",
				handleRemarks: "",
				assetChangeRecordVoList: [],
				changeId: ""
			},
			rules2: {
				disposalOrderNo: [
					{
						required: true,
						message: "请输入处置单号",
						trigger: "blur"
					},
					{ validator: validatorDisposalOrderNo, trigger: "blur" }
				],
				disposalType: [
					{
						required: true,
						message: "请选择处置类型",
						trigger: "blur"
					}
				],
				//校验金额或者面积
				residualIncome: [
					{
						required: true,
						message: "请输入残值收入",
						trigger: "change"
					},
					{
						pattern: /^(([1-9]{1}\d*)|(0{1}))(\.\d{1,2})?$/,
						message: "请输入合法的数字，最多两位小数",
						trigger: "change"
					}
				],

				disposalReason: [
					{
						required: true,
						message: "请输入处置原因",
						trigger: "blur"
					}
				],

				handleRemarks: [
					{ required: true, message: "请输入描述", trigger: "blur" }
				]
			}
		};
	},
	/**
	 * 载入事件
	 */
	mounted() {
		//根据主键查询已入库的信息
		this.getWarehousing();
		//获取处置信息

		if (this.$route.query.assetHandleId != null) {
			this.getHandle();
		}
	},
	computed: {
		...mapGetters({
			cityAssembly: "getCityAssembly",
			districtCollection: "getDistrictCollection"
		})
	},
	methods: {
		getDict(v) {
			return getDict(v);
		},
		/**
		 * 根据主键查询已入库的信息
		 */
		getWarehousing() {
			this.loading = true;
			seacrhWarehousing(this.$route.query.assetFixedWarehousingId)
				.then(resp => {
					let obj = resp.data.data;
					this.formObj = obj;
					this.government = this.formObj.assetRegistrationId == "1" ? "1" : "0";
					this.formObj2.voucherNo = obj.voucherNo;
					this.formObj2.voucherDateStart = obj.voucherDateStart;
					this.formObj2.voucherDateEnd = obj.voucherDateEnd;
					this.loading = false;
				})
				.catch(error => {
					this.$message({
						type: "error",
						message: "获取物业入库信息异常"
					});
				})
				.finally(() => (this.loading = false));
		},
		/**
		 * 根据主键查询已入库的信息
		 */
		getHandle() {
			this.loading = true;
			getAssetHandle(this.$route.query.assetHandleId)
				.then(resp => {
					this.formObj2 = resp.data.data;
					this.loading = false;
				})
				.catch(error => {
					this.$message({
						type: "error",
						message: "获取处置信息异常"
					});
				})
				.finally(() => (this.loading = false));
		},

		/**
		 * 保存按钮 触发文件上传
		 */
		sendData() {
			this.loading = true;
			this.$refs["ruleForm2"].validate(valid2 => {
				if (valid2) {
					this.$refs.handleFile.submit(this.modelType);
				} else {
					this.loading = false;
					return false;
				}
			});
		},

		/**
		 * 保存表单信息
		 * @returns {Promise<void>}
		 */
		saveEntity(val) {
			if (val) this.formObj2.changeId = val.data;

			if (!this.formObj2.approveContent)
				this.formObj2.approveContent = "发起处置";

			if (this.$route.query.assetHandleId == null) {
				addAssetHandle(this.formObj2)
					.then(data => {
						this.$message.success("操作成功");
						//操作完成 跳转页面
						this.$router.push("/asset-management/handle-list");
					})
					.catch(error => {
						this.$message.error("操作失败");
						this.formObj2.assetHandleId = null;
					})
					.finally(() => (this.loading = false));
			} else {
				updateAssetHandle(this.formObj2)
					.then(data => {
						this.$message.success("操作成功");
						//操作完成 跳转页面
						this.$router.push("/asset-management/handle-list");
					})
					.catch(error => {
						this.$message.error("操作失败");
					})
					.finally(() => (this.loading = false));
			}
		},

		//返回
		goBack() {
			this.$router.push("/asset-management/handle-list");
		}
	}
};
